﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using System.Xml;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.Controls;
using YArchitech.LIB;
using YArchitech.Revit.Electric.Common;
using HYRElectric.Equipment;

namespace HYRElectric.PowerSystem
{
	public partial class DlgEquipmentKeyWordSet : System.Windows.Forms.Form
	{
		public DlgEquipmentKeyWordSet(ExternalCommandData cmdData, Dictionary<FamilyInstance, string> dictFamilyInstanceSel)
		{
			this.InitializeComponent();
			this.m_cmdData = cmdData;
			this.m_dictFamilyInstanceSel = dictFamilyInstanceSel;
		}

		private void DlgEquipmentKeyWordSet_Load(object sender, EventArgs e)
		{
			this.btnAdd.Image = Resource.GetImage("AddNew16");
			this.btnDel.Image = Resource.GetImage("delete16");
			if (!this.ReadConfigurationFile())
			{
				return;
			}
			this.InitialKeyWordRecord();
			this.InitialKeyWordView();
			if (0 < this.dataGridView1.Rows.Count)
			{
				this.btnDel.Enabled = true;
				return;
			}
			this.btnDel.Enabled = false;
		}

		private bool ReadConfigurationFile()
		{
			try
			{
				this.m_xmlDoc.Load(this.m_strFilePath);
			}
			catch
			{
				MessageBox.Show("缺少关键字设置配置文件");
				return false;
			}
			return true;
		}

		private void InitialKeyWordRecord()
		{
			XmlNodeList xmlNodeList = this.m_xmlDoc.SelectSingleNode("KewWordSet").SelectNodes("KewWord");
			if (xmlNodeList != null)
			{
				foreach (object obj in xmlNodeList)
				{
					XmlElement xmlElement = ((XmlNode)obj) as XmlElement;
					KeyWordSet keyWordSet = new KeyWordSet();
					keyWordSet.KewWord = xmlElement.GetAttribute("Name");
					XmlNode xmlNode = xmlElement.SelectSingleNode("Item");
					keyWordSet.OrigonEquipType = (xmlNode as XmlElement).GetAttribute("OrigonEquipType");
					keyWordSet.EquipName = (xmlNode as XmlElement).GetAttribute("EquipName");
					keyWordSet.ClassName = (xmlNode as XmlElement).GetAttribute("Category");
					keyWordSet.FamilyPath = (xmlNode as XmlElement).GetAttribute("FamilyPath");
					keyWordSet.EquipType = (xmlNode as XmlElement).GetAttribute("EquipType");
					this.m_lstKeywordInfo.Add(keyWordSet);
				}
			}
		}

		private void InitialKeyWordView()
		{
			this.dataGridView1.RowHeadersVisible = false;
			this.dataGridView1.ColumnHeadersVisible = true;
			int num = 0;
			DataGridViewTextBoxColumn dataGridViewTextBoxColumn = new DataGridViewTextBoxColumn();
			dataGridViewTextBoxColumn.Name = "KeyWord";
			dataGridViewTextBoxColumn.HeaderText = "原设备名称关键字";
			dataGridViewTextBoxColumn.Width = 130;
			this.dataGridView1.Columns.Insert(num++, dataGridViewTextBoxColumn);
			dataGridViewTextBoxColumn = new DataGridViewTextBoxColumn();
			dataGridViewTextBoxColumn.Name = "NameNew";
			dataGridViewTextBoxColumn.HeaderText = "替换为";
			dataGridViewTextBoxColumn.Width = 130;
			this.dataGridView1.Columns.Insert(num++, dataGridViewTextBoxColumn);
			dataGridViewTextBoxColumn = new DataGridViewTextBoxColumn();
			dataGridViewTextBoxColumn.Name = "ClassName";
			dataGridViewTextBoxColumn.HeaderText = "归属类别";
			dataGridViewTextBoxColumn.Width = 80;
			this.dataGridView1.Columns.Insert(num++, dataGridViewTextBoxColumn);
			this.dataGridView1.Rows.Clear();
			foreach (KeyWordSet keyWordSet in this.m_lstKeywordInfo)
			{
				int index = this.dataGridView1.Rows.Add();
				this.dataGridView1.Rows[index].Cells[0].Value = keyWordSet.KewWord;
				this.dataGridView1.Rows[index].Cells[1].Value = keyWordSet.EquipName;
				this.dataGridView1.Rows[index].Cells[1].ReadOnly = true;
				DataGridViewButtonCell dataGridViewButtonCell = new DataGridViewButtonCell();
				dataGridViewButtonCell.Value = keyWordSet.ClassName;
				this.dataGridView1.Rows[index].Cells[2] = dataGridViewButtonCell;
			}
		}

		private void SaveData()
		{
			if (this.m_lstKeywordInfo != null)
			{
				XmlNode xmlNode = this.m_xmlDoc.SelectSingleNode("KewWordSet");
				xmlNode.RemoveAll();
				foreach (KeyWordSet keyWordSet in this.m_lstKeywordInfo)
				{
					XmlElement xmlElement = this.m_xmlDoc.CreateElement("KewWord");
					xmlElement.SetAttribute("Name", keyWordSet.KewWord);
					XmlElement xmlElement2 = this.m_xmlDoc.CreateElement("Item");
					xmlElement2.SetAttribute("OrigonEquipType", keyWordSet.OrigonEquipType);
					xmlElement2.SetAttribute("EquipName", keyWordSet.EquipName);
					xmlElement2.SetAttribute("Category", keyWordSet.ClassName);
					xmlElement2.SetAttribute("FamilyPath", keyWordSet.FamilyPath);
					xmlElement2.SetAttribute("EquipType", keyWordSet.EquipType);
					xmlElement.AppendChild(xmlElement2);
					xmlNode.AppendChild(xmlElement);
				}
				this.m_xmlDoc.Save(this.m_strFilePath);
			}
		}

		private EquipmentType GetEquipmentType(string strType)
		{
			EquipmentType result = EquipmentType.Power;
			if ("灯具" == strType)
			{
				result = EquipmentType.Lamp;
			}
			else if ("开关" == strType)
			{
				result = EquipmentType.Switch;
			}
			else if ("插座" == strType)
			{
				result = EquipmentType.Socket;
			}
			else if ("配电箱" == strType)
			{
				result = EquipmentType.SwitchBox;
			}
			else if ("动力" == strType)
			{
				result = EquipmentType.Power;
			}
			else if ("探测器" == strType)
			{
				result = EquipmentType.FireDetector;
			}
			else if ("消防报警" == strType)
			{
				result = EquipmentType.FireAlarm;
			}
			else if ("广播" == strType)
			{
				result = EquipmentType.Radio;
			}
			else if ("安防" == strType)
			{
				result = EquipmentType.Security;
			}
			else if ("综合布线" == strType)
			{
				result = EquipmentType.Cabling;
			}
			return result;
		}

		private FileOperation FilterEquipment(string strOldEquipName, string strNewEquipName)
		{
			FileOperation result = FileOperation.FO_Add;
			foreach (KeyWordSet keyWordSet in this.m_lstKeywordInfo)
			{
				if (strOldEquipName == keyWordSet.KewWord)
				{
					if (strNewEquipName == keyWordSet.EquipName)
					{
						result = FileOperation.FO_None;
						break;
					}
					result = FileOperation.FO_Replace;
					break;
				}
			}
			return result;
		}

		private string GetOrigonEquipType(string strKeyword)
		{
			string result = "";
			foreach (KeyValuePair<FamilyInstance, string> keyValuePair in this.m_dictFamilyInstanceSel)
			{
				FamilyInstance key = keyValuePair.Key;
				string value = keyValuePair.Value;
				FamilySymbol symbol = key.Symbol;
				if (symbol.FamilyName + "/" + symbol.Name == strKeyword)
				{
					result = value;
				}
			}
			return result;
		}

		private void ModifyKeywordList(KeyWordSet key)
		{
			for (int i = 0; i < this.m_lstKeywordInfo.Count; i++)
			{
				if (this.m_lstKeywordInfo.ElementAt(i).KewWord == key.KewWord)
				{
					this.m_lstKeywordInfo.RemoveAt(i);
					this.m_lstKeywordInfo.Insert(i, key);
					return;
				}
			}
		}

		private void UpdateKeywordSetFile(string strOldName, string strNewName)
		{
			for (int i = 0; i < this.m_lstKeywordInfo.Count; i++)
			{
				KeyWordSet keyWordSet = this.m_lstKeywordInfo.ElementAt(i);
				if (keyWordSet.KewWord == strOldName)
				{
					this.m_lstKeywordInfo.Remove(keyWordSet);
					keyWordSet.KewWord = strNewName;
					this.m_lstKeywordInfo.Insert(i, keyWordSet);
					return;
				}
			}
		}

		private void btnAdd_Click(object sender, EventArgs e)
		{
			int index = this.dataGridView1.Rows.Add();
			this.dataGridView1.Rows[index].Cells[0].Value = "";
			this.dataGridView1.Rows[index].Cells[1].Value = "请进行设置→";
			this.dataGridView1.Rows[index].Cells[1].ReadOnly = true;
			DataGridViewButtonCell dataGridViewButtonCell = new DataGridViewButtonCell();
			dataGridViewButtonCell.Value = this.m_strConfigCellString;
			this.dataGridView1.Rows[index].Cells[2] = dataGridViewButtonCell;
			this.btnDel.Enabled = true;
		}

		private void btnDel_Click(object sender, EventArgs e)
		{
			if (this.dataGridView1.SelectedRows.Count != 0)
			{
				DataGridViewRow dataGridViewRow = this.dataGridView1.SelectedRows[0];
				this.dataGridView1.Rows.Remove(dataGridViewRow);
				foreach (KeyWordSet keyWordSet in this.m_lstKeywordInfo)
				{
					if (keyWordSet.KewWord == dataGridViewRow.Cells[0].Value.ToString())
					{
						this.m_lstKeywordInfo.Remove(keyWordSet);
						break;
					}
				}
			}
			if (0 >= this.dataGridView1.Rows.Count)
			{
				this.btnDel.Enabled = false;
				return;
			}
			this.btnDel.Enabled = true;
		}

		private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
		{
			int count = this.dataGridView1.Rows.Count;
			if (e.ColumnIndex == 0 && 0 < count && "" != e.FormattedValue.ToString())
			{
				for (int i = 0; i < count; i++)
				{
					DataGridViewRow dataGridViewRow = this.dataGridView1.Rows[i];
					if (e.RowIndex != i && e.FormattedValue.ToString() == dataGridViewRow.Cells[0].Value.ToString())
					{
						MessageBox.Show("原设备名称关键字不能重复", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
						e.Cancel = true;
					}
				}
			}
		}

		private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
		{
			if (2 == e.ColumnIndex && 0 <= e.RowIndex)
			{
				string value = "";
				string text = this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
				EquipmentType type;
				if (this.m_strConfigCellString == text)
				{
					type = EquipmentType.Lamp;
				}
				else
				{
					type = this.GetEquipmentType(text);
				}
				DbFamilyObject selectFamily = new EquipmentLayoutCmdEntry(this.m_cmdData, type).GetSelectFamily(ref value, ref type);
				if (selectFamily == null)
				{
					return;
				}
				this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex - 1].Value = selectFamily.Name;
				this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex - 1].Tag = selectFamily;
				this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = value;
			}
		}

		private void btnOK_Click(object sender, EventArgs e)
		{
			if (0 >= this.dataGridView1.Rows.Count)
			{
				this.m_lstKeywordInfo.Clear();
			}
			else
			{
				for (int i = 0; i < this.dataGridView1.Rows.Count; i++)
				{
					if (this.dataGridView1.Rows[i].Cells[0].Value == null || "" == this.dataGridView1.Rows[i].Cells[0].Value.ToString())
					{
						this.dataGridView1.Rows[i].Cells[0].Selected = true;
						YJKMessageBox.Show("没有设置原设备名称关键字！");
						return;
					}
					KeyWordSet keyWordSet = new KeyWordSet();
					keyWordSet.KewWord = this.dataGridView1.Rows[i].Cells[0].Value.ToString();
					keyWordSet.OrigonEquipType = this.GetOrigonEquipType(keyWordSet.KewWord);
					keyWordSet.EquipName = this.dataGridView1.Rows[i].Cells[1].Value.ToString();
					keyWordSet.ClassName = this.dataGridView1.Rows[i].Cells[2].Value.ToString();
					if (string.Equals(keyWordSet.ClassName, this.m_strConfigCellString))
					{
						this.dataGridView1.Rows[i].Cells[0].Selected = true;
						YJKMessageBox.Show("没有设置归属类别！");
						return;
					}
					DbFamilyObject dbFamilyObject = this.dataGridView1.Rows[i].Cells[1].Tag as DbFamilyObject;
					if (dbFamilyObject != null)
					{
						keyWordSet.FamilyPath = dbFamilyObject.Family;
						string strType = this.dataGridView1.Rows[i].Cells[2].Value.ToString();
						EquipmentType equipmentType = this.GetEquipmentType(strType);
						keyWordSet.EquipType = Convert.ToInt32(equipmentType).ToString();
					}
					FileOperation fileOperation = this.FilterEquipment(keyWordSet.KewWord, keyWordSet.EquipName);
					if (fileOperation != FileOperation.FO_Add)
					{
						if (fileOperation == FileOperation.FO_Replace)
						{
							this.ModifyKeywordList(keyWordSet);
						}
					}
					else
					{
						this.m_lstKeywordInfo.Add(keyWordSet);
					}
				}
			}
			this.SaveData();
			base.DialogResult = DialogResult.OK;
		}

		private void dataGridView1_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
		{
			if (e.ColumnIndex == 0 && 0 <= e.RowIndex && this.dataGridView1.Rows[e.RowIndex].Cells[0].Value != null)
			{
				this.m_strCellValue = this.dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();
			}
		}

		private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
		{
			if (e.ColumnIndex == 0 && 0 <= e.RowIndex && this.dataGridView1.Rows[e.RowIndex].Cells[0].Value != null)
			{
				string text = this.dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();
				if (text != this.m_strCellValue)
				{
					this.UpdateKeywordSetFile(this.m_strCellValue, text);
				}
			}
		}

		private string m_strFilePath = Product.DataLocation + "\\ElectricalData\\KewWordSet.xml";

		private List<KeyWordSet> m_lstKeywordInfo = new List<KeyWordSet>();

		public XmlDocument m_xmlDoc = new XmlDocument();

		private ExternalCommandData m_cmdData;

		private Dictionary<FamilyInstance, string> m_dictFamilyInstanceSel = new Dictionary<FamilyInstance, string>();

		private string m_strCellValue = "";

		private string m_strConfigCellString = "匹配设置";
	}
}
